<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Controller extends CController
{
	
	public function filters(){  
		
	    return array(
	        'accessControl', // perform access control for CRUD operations
	    );
	}
	
	public function smarty_init($modules = '',$action,$csses=array(),$jses=array())
	{
		$app_path = Yii::app()->basePath;
		$smarty = Yii::app()->smarty;
                $smarty->assign('csses',$csses);
                $smarty->assign('jses',$jses);
        if($modules != ""){
			$smarty->template_dir = $app_path."/modules/" . $modules . "/views/".$action;
        }else{
			$smarty->template_dir = $app_path."/views/".$action;
        }
		//print_r($smarty->template_dir);exit;
		return $smarty;
	}
	    
	function smartyRenderPartial($view,$data=null,$modules,$action,$return=false,$processOutput=false,$cache=array()){
		$app_path = Yii::app()->basePath;
	 	if($modules != ""){
			$template_dir = $app_path."/modules/" . $modules . "/views/".$action;
        }else{
			$template_dir = $this->getViewPath();
        }
		$view = $template_dir."/" .$view;
		$smarty = Yii::app()->smarty;

		if (is_array($cache)&&count($cache)>0) {
			$smarty->caching = true;
			$smarty->cache_lifetime = $cache['cache_lifetime'];
			$smarty->cache_dir = $cache['cache_dir'];
		}else{
			$smarty->caching = false;
		}

		$smarty->assign("baseURL", BASE_URL);
		
		$smarty->template_dir = $template_dir;
		if(is_file($view)!==false){
			if($data != null){
				foreach($data as $key=>$value){
					$smarty->assign($key,$value);
				}
			}
			// $output = $smarty->fetch($view);   
			if (is_array($cache)&&count($cache)>0&&$cache['param']>''){
                $output = $smarty->fetch($view,$cache['param']);  
	        }else{
	           	$output = $smarty->fetch($view);
	        }
	        
			if($return)
				return $output;
			else
				echo $output;
			}
		else
			throw new CException(Yii::t('yii','{controller} cannot find the requested view "{view}".',
			array('{controller}'=>get_class($this), '{view}'=>$view)));
	}
        
	function smartyRender($view,$data=null,$modules = '',$action='',$return=false,$processOutput=false,$cache=array()){
		$smarty = Yii::app()->smarty;
		$smarty->template_dir = $this->getViewPath();
		if (is_array($cache)&&count($cache)>0){
			$smarty->caching = true;
			$smarty->cache_lifetime = $cache['cache_lifetime'];
			$smarty->cache_dir = $cache['cache_dir'];
		}else{
			$smarty->caching = false;
		}
		echo $this->smartyRenderPartial($view,$data,$modules ,$action,$return,$processOutput,$cache);
	}
}